package org.fusionide.builder.parsers.cfml.tags;

import java.util.Vector;

import org.fusionide.builder.parsers.IParserHandler;
import org.fusionide.builder.parsers.ParserException;
import org.fusionide.builder.parsers.cfml.CFMLTag;
import org.fusionide.builder.parsers.cfml.Variable;

public class Catch extends CFMLTag {

	private String type;

	private boolean requireClosingTag = true;

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public Catch(String attributes, int startOffset) {
		super(attributes, startOffset);

		if (attributes == null)
			return;

		this.type = getAttribute("type");
	}

	@Override
	public boolean requiresClosingTag() {
		return requireClosingTag;
	}

	@Override
	public boolean acceptsChildren(CFMLTag tag) {
		return super.acceptsChildren(tag);
	}

	@Override
	public Vector<Variable> getVariables(int offset) {
		return super.getChildVariables(offset);
	}
	
	@Override
	public Vector<Variable> getChildVariables(int offset) {
		Vector<Variable> vars = new Vector<Variable>();
		vars.add(new Variable("cfcatch", startLine, true, Variable.struct));
		vars.add(new Variable("cfcatch.type", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.message", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.detail", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.tagcontext", startLine, true, Variable.array));
		vars.add(new Variable("cfcatch.nativeerrorcode", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.sqlstate", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.sql", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.queryerror", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.where", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.errnumber", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.missingfilename", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.lockname", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.lockoperation", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.errorcode", startLine, true, Variable.string));
		vars.add(new Variable("cfcatch.extendedinfo", startLine, true, Variable.string));
		vars.addAll(super.getChildVariables(offset));
		return vars;
	}

	@Override
	public boolean validate(IParserHandler parserHandler) {
		if (super.validate(parserHandler)) {
			if (!(hasParent() && parent instanceof Try)) {
				if (parserHandler != null)
					parserHandler.addMarker(new ParserException("<cfcatch> Needs to have <cftry> as parent", ParserException.SEVERITY_ERROR, startLine));
				return false;
			}
			return true;
		}
		return false;
	}

	@Override
	public void closeTag(int offset, int linenumber, IParserHandler parserHandler) {
		super.closeTag(offset, linenumber, parserHandler);
		requireClosingTag = false;
	}
}
